﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;


namespace ListaTelefonicaMEM
{
    class LabelPadrao : Label
    {
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.Font = new Font(Config.NomeFonte, Config.TamanhoFonte);
        }
    }

    class TextBoxPadrao : TextBox
    {
        protected AutoCompleteStringCollection src = new AutoCompleteStringCollection();

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.Font = new Font(Config.NomeFonte, Config.TamanhoFonte);
        }

        public void RefreshDataSource()
        {
            lt.FListaEmpresas.OrderBy(p => p.Nome);

            src.Clear();

            src.AddRange(lt.FListaEmpresas.Select(p => p.Nome).ToArray());
            this.AutoCompleteCustomSource = src;
            this.AutoCompleteSource = AutoCompleteSource.CustomSource;
            this.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            this.SelectionStart = 0;
            this.SelectionLength = base.Text.Length;
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (e.KeyData == Keys.Return)
            {
                SendKeys.Send("{TAB}");
            }
        }
    }

    class MaskedTextBoxPadrao : MaskedTextBox
    {
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.Font = new Font(Config.NomeFonte, Config.TamanhoFonte);
        }

        public string Digits()
        {
            string res = null;

            foreach (char item in base.Text)
            {
                if (char.IsDigit(item))
                {
                    res += item;
                }
            }

            return res;
        }

        public string TextoComMascara()
        {
            string digitos = this.Digits().PadLeft(11, '0');
            string res = null;

            res += "(" + digitos.Substring(0, 3) + ") ";
            res += digitos.Substring(3, 4) + "-";
            res += digitos.Substring(7, 4);

            return res;
        }
    }

    class TreeViewPadrao : TreeView
    {
        private int levelEmpresa = 1;

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.Font = new Font(Config.NomeFonte, Config.TamanhoFonte);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (e.KeyData == Keys.F5)
            {
                this.RefreshDados();
            }
            else if (e.KeyData == Keys.Delete)
            {
                DialogResult res = MessageBox.Show("Deseja excluir o registro ?", "Excluir", MessageBoxButtons.YesNo);

                if (res == DialogResult.Yes)
                {
                    if (this.SelectedNode.Level == levelEmpresa)
                    {
                        string str = this.SelectedNode.Text;

                        lt.DeletarEmpresa(str);
                    }
                    else if (this.SelectedNode.Level > levelEmpresa)
                    {
                        string strTel = this.SelectedNode.Text;
                        string strEmp = this.SelectedNode.Parent.Text;

                        lt.DeletarTelefone(strEmp, strTel);
                    }
                    this.SelectedNode.Remove();
                }
            }
        }

        public void RefreshDados()
        {
            lt.FListaEmpresas.OrderBy(p => p.Nome);

            List<char> digitos = new List<char>();
            
            this.Nodes.Clear();
            this.BeginUpdate();
            foreach (Empresa emp in lt.FListaEmpresas)
            {
                if (digitos.Count(d => d == emp.Nome[0]) == 0)
                {
                    digitos.Add(emp.Nome[0]);
                    this.Nodes.Add(emp.Nome[0].ToString(), digitos[digitos.FindIndex(d => d == emp.Nome[0])].ToString());
                }

                this.Nodes[emp.Nome[0].ToString()].Nodes.Add(emp.Indice.ToString(), emp.Nome);

                foreach (Telefone tel in lt.FListaTelefones.FindAll(t => t.IndiceEmpresa == emp.Indice).ToList())
                {
                    this.Nodes[emp.Nome[0].ToString()].Nodes[emp.Indice.ToString()].Nodes.Add(tel.Indice.ToString(), tel.Numero);
                }
            }
            this.EndUpdate();
        }
    }

    class ComboBoxPadrao : ComboBox
    {
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            this.Font = new Font(Config.NomeFonte, Config.TamanhoFonte);
        }
    }

    class ComboBoxNome : ComboBoxPadrao
    {
        public void RefreshCustomSource()
        {

        }
    }

    class TextBoxIndice : TextBoxPadrao
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            if (char.IsDigit(e.KeyChar))
            { }
            else if (e.KeyChar == '\b')
            { }
            else if (e.KeyChar == (char)Keys.Space)
            {
                this.Text = "";
            }
            else if (e.KeyChar == (char)Keys.Escape)
            { }
            else
            {
                e.Handled = true;
            }
        }

        public int Valor
        {
            get
            {
                return Int32.Parse(this.Text);
            }
        }
    }


}
